package br.project.cdd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import br.project.cdd.bean.PermissaoBean;

public class PermissaoDAO extends Conexao {
	
	public PermissaoDAO(){
		super();
	}
	
	public PermissaoBean buscarPermissao(int idPerfil, int idTela){
		
		PermissaoBean bean = new PermissaoBean();
		
		try{
			String sql = "SELECT PR.ID_PERMISSAO FROM PERMISSAO PR, PERFIL PE, TELAS TE WHERE PR.ID_PERFIL = PE.ID_PERFIL AND PR.ID_TELA = TE.ID_TELA AND PE.ID_PERFIL=" + idPerfil + " AND PR.ID_TELA=" + idTela + ";";
			Statement stmt = conexao.createStatement();
			ResultSet consulta = stmt.executeQuery(sql);
			
			while (consulta.next()){
				if (String.valueOf(consulta.getInt(1)) == null)
					bean.setIdPermissao(null);
				else
					bean.setIdPermissao(String.valueOf(consulta.getInt(1)));
			}
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
		
		return bean;
	}
	
	public void inserirPermissao(int idPerfil, int idTela){
		
		try{
			String sql = "INSERT INTO PERMISSAO (ID_PERFIL, ID_TELA) VALUES (" + idPerfil + ", " + idTela + ")";
			Statement stmt = conexao.createStatement();
			stmt.executeUpdate(sql);
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
	}
	
	public void deletarPermissao(int idPerfil){
		
		try{
			String sql = "DELETE FROM PERMISSAO WHERE ID_PERFIL = " + idPerfil;
			Statement stmt = conexao.createStatement();
			stmt.executeUpdate(sql);
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
	}
	
}
